package com.xm.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xm.common.entity.R;
import com.xm.web.consts.MagicNumEnum;
import com.xm.web.entity.po.TbPermission;
import com.xm.web.service.ITbPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author yousj
 * @since 2021/8/29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/menu")
@Api(tags = "菜单")
public class MenuController {

    private final ITbPermissionService permissionService;

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public R list(String permissionName) {
        return R.isOk(permissionService.list(Wrappers.<TbPermission>lambdaQuery()
                .eq(TbPermission::getPermissionType, MagicNumEnum.PERMISSION_MENU.getCode())
                .like(Objects.nonNull(permissionName), TbPermission::getPermissionName, permissionName)
        ));
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R add(@RequestBody TbPermission request) {
        return R.isOk(permissionService.save(request));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public R update(@RequestBody TbPermission request) {
        return R.isOk(permissionService.updateById(request));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        return R.isOk(permissionService.removeById(id));
    }


}
